package com.shanghaionstar.nav.presenter;

import com.onstar.cn.common.exception.OnStarException;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.framework.utils.RxBus;
import com.onstar.cn.mag.enumtype.RemoteCommandsType;
import com.onstar.cn.sqlite.entities.PoiSearchHistory;
import com.onstar.cn.map.common.PoiItemInfo;
import com.onstar.cn.rxconstants.RxConstants;
import com.shanghaionstar.enumtype.FunctionNameFindId;
import com.shanghaionstar.nav.iview.INavMainView;
import com.shanghaionstar.nav.model.NavInfoModel;
import com.shanghaionstar.nav.model.PoiHistoryInfo;
import com.shanghaionstar.nav.service.AbstractMapActivity;
import com.shanghaionstar.nav.service.NavService;
import com.shanghaionstar.sqlite.SearchHistoryImpl;
import com.shanghaionstar.sqlite.TBTHistoryImpl;
import com.shanghaionstar.utils.BusinessUtil;
import com.shanghaionstar.utils.NavHolder;

import rx.functions.Action1;

/**
 * Created by 6335yz on 2016/7/7.
 */
public class NavSearchHistoryPresenter extends BasePresenter {

    private AbstractMapActivity mapActivity;
    private INavMainView navMainView;
    private NavService navService;


    @Override
    public void setView(IMVPView view) {
        super.setView(view);
        navMainView = (INavMainView) view;
    }

    public NavSearchHistoryPresenter(IMVPScheduler mvpScheduler, AbstractMapActivity mapActivity) {
        super(mvpScheduler);
        this.mapActivity = mapActivity;
        navService = new NavService(mapActivity);
    }

    public void sendTbt(PoiSearchHistory poiSearchHistory) {
//        BusinessUtil.sendReportByClickType(mapActivity, FunctionNameFindId.Searchpoi_history_navigation.getStringValue());
        PoiItemInfo poiItemInfo = new PoiItemInfo();
        poiItemInfo.setTitle(poiSearchHistory.getName());
        poiItemInfo.setThreadId(poiSearchHistory.getSearchtime());
        poiItemInfo.setAddress(poiSearchHistory.getAddress());
        poiItemInfo.setLatitude(poiSearchHistory.getLat());
        poiItemInfo.setLongitude(poiSearchHistory.getLon());
        poiItemInfo.setCityName(poiSearchHistory.getCityname());
        poiItemInfo.setTelphone(poiSearchHistory.getPhone());
        NavInfoModel navInfoModel = new NavInfoModel();
        navInfoModel.remoteCommandsType = RemoteCommandsType.COMMANDS_TBT_ROUTE;
        navInfoModel.poiItemInfo = poiItemInfo;
        doNavCommand(navInfoModel);
    }

    private void doNavCommand(final NavInfoModel navInfoModel) {
        BusinessUtil.sendReportByClickType(mapActivity, FunctionNameFindId.Mytrip_Navigation.getStringValue());
        subscription = navService.doNavCommand(navInfoModel)
                .subscribeOn(mvpScheduler.backgroundThread())
                .observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String requestId) {
                        if (navInfoModel.remoteCommandsType == RemoteCommandsType.COMMANDS_NAV_DESTINATION || navInfoModel.remoteCommandsType == RemoteCommandsType.COMMANDS_TBT_ROUTE) {
                            saveHistoryToSql(requestId, navInfoModel.poiItemInfo, null, true);
                            //将下发历史保存到历史记录中
                            saveSearchHistory(navInfoModel.poiItemInfo);
                        }
                        if(navMainView!=null) {
                            navMainView.showCommandLoading(true);
                        }
                        NavHolder.getInstance(mapActivity).setCurrentSendPoi(navInfoModel.poiItemInfo);

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        //里面抛出的Error信息
                        if (navMainView != null) {
                            navMainView.onResponseError((OnStarException) throwable);
                        }
                    }
                });
    }

    public void saveHistoryToSql(String requestId, PoiItemInfo poiItemInfo, String status, boolean isSave) {
        PoiHistoryInfo historyInfo = new PoiHistoryInfo();
        historyInfo.setPoiItemInfo(poiItemInfo);
        if (isSave) {
            historyInfo.setStatus("inProgress");
        } else {
            historyInfo.setStatus(status);
        }
        TBTHistoryImpl impl = new TBTHistoryImpl(mapActivity);
        impl.saveOrUpdateModelDestinationHistory(requestId, historyInfo, isSave);
    }


    /**
     * @param
     * @return void
     * @Title: saveSearchHistory
     * @Description: 保存到历史记录中
     */
    public void saveSearchHistory(PoiItemInfo poiItemInfo) {
        SearchHistoryImpl impl = new SearchHistoryImpl(mapActivity);
        impl.saveSearchHistory(poiItemInfo);
        RxBus.getDefault().post(RxConstants.ALL_PAGE_REFRESH, RxConstants.NAV_SAVE_POI);
    }
}
